package appapi.controller.oider;

import appapi.Vo.EditSysConfigVo;
import appapi.common.BusinessTypeEnum;
import appapi.common.ResponseBean;
import appapi.config.MyLog;
import appapi.entity.basic.SysConfigEntity;
import appapi.service.SysConfigService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.time.LocalDateTime;

/**
 * 请求对象
 *
 * @author luocheng
 * @version 1.0
 * @date 2023/05/27 15:53
 */
@Api(tags = "基础数据-系统设置")
@RestController
@RequestMapping("/api/sysConfig")
public class SysConfigController {
    private static final Logger logger = LoggerFactory.getLogger(SysConfigController.class);
    @Autowired
    private SysConfigService sysConfigService;
    /**
     * 信息
     */
    @GetMapping("/getDetail")
    @ApiOperation(value = "获取详情")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", name = "confId", dataType = "int", value = "设置Id",required = true)
    })
    public ResponseBean detail(@Valid Integer confId) {
        try {
            if (confId == null) {
                return ResponseBean.fail("ID不能为空");
            }
            SysConfigEntity entity = sysConfigService.detail(confId);
            if (entity != null) {
                return ResponseBean.success(entity);
            } else {
                return ResponseBean.fail("未查询到记录");
            }
        } catch (Exception e) {
            logger.error("获取详情异常", e);
            return ResponseBean.fail("获取详情异常");
        }
    }

    /**
     * 修改
     */
    @ApiOperation(value = "修改设置")
    @PostMapping(value = "/editInfo")
    @MyLog(title = "修改系统设置信息",businessType = BusinessTypeEnum.update)
    public ResponseBean update(@Validated  @RequestBody EditSysConfigVo vo) {
        try {
            SysConfigEntity entity = vo.vo2Entity();
            entity.setUpdateTime(LocalDateTime.now());
            int iRet = sysConfigService.updateByPrimaryKey(entity);
            if (iRet > 0) {
                return ResponseBean.success("更新成功");
            } else {
                return ResponseBean.fail("更新失败");
            }
        } catch (Exception e) {
            logger.error("更新异常", e);
            return ResponseBean.fail("更新异常");
        }
    }

}
